/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package menu;

import czStvDpds.centrallogic.Logic;
import czStvDpds.graphics3D.Drawing;
import czStvDpds.graphics3D.GraphicsIndex;
import czStvDpds.graphics3D.gui.ButtonEvent;
import czStvDpds.graphics3D.gui.ButtonListener;
import czStvDpds.graphics3D.gui.LButton;
import czStvDpds.graphics3D.gui.LCardContainer;
import czStvDpds.graphics3D.gui.LComponent;
import czStvDpds.graphics3D.gui.LGridPanel;
import czStvDpds.graphics3D.gui.LLabel;
import czStvDpds.graphics3D.gui.LTextField;
import czStvDpds.graphics3D.gui.SuperContainer;
import java.net.InetAddress;

/**
 *
 * @author Ondra
 */
public class PausedGameMenu implements ButtonListener{
    
    private static LCardContainer container;
    private static PausedGameMenu gameMenu;
    private static Drawing d;
    private static boolean initialized = false;
    
    public static void init(Logic logic){
        d = GraphicsIndex.getDrawing();
        container = SuperContainer.create(SuperContainer.GAME_MENU, d.getGUIScene(SuperContainer.GAME_MENU));
        gameMenu = new PausedGameMenu(logic);
        
        initialized = true;
    }
    
    public static void toggleVisibility(){
        if(!initialized){
           throw new UnsupportedOperationException("menu not initialized");
        }
        SuperContainer.switchContainer(SuperContainer.GAME_MENU);
        d.setGUISceneVisibility(SuperContainer.GAME_MENU, !d.getGUISceneVisibility(SuperContainer.GAME_MENU));

        container.showComponent(0);
    }
    
    private LButton networkButton, exitGameButton, connectOkayButton, cancelButton, nameButton, nameOkayButton;
    private LTextField portField, addressField, nameField;
    private Logic logic;
    
    private PausedGameMenu(Logic logic){
        this.logic = logic;
        
        networkButton = new LButton("Connect to a network game");
        networkButton.addButtonListener(this);
        
        exitGameButton = new LButton("Exit DPDS");
        exitGameButton.addButtonListener(this);
        
        connectOkayButton = new LButton("Connect to server");
        connectOkayButton.addButtonListener(this);
        
        cancelButton = new LButton("Cancel");
        cancelButton.addButtonListener(this);
        
        nameButton = new LButton("Change player");
        nameButton.addButtonListener(this);
        
        nameOkayButton = new LButton("Save name");
        nameOkayButton.addButtonListener(this);
        
        
        addressField = new LTextField("localhost");
        addressField.addButtonListener(this);
        
        nameField = new LTextField();
        nameField.addButtonListener(this);
        
        LGridPanel connectScreen = new LGridPanel(4,1);
        LGridPanel normalScreen = new LGridPanel(3,1);
        LGridPanel nameScreen = new LGridPanel(4,1);
        
        container.addComponent(normalScreen, 0);
        normalScreen.addComponent(networkButton);
        normalScreen.addComponent(nameButton);
        normalScreen.addComponent(exitGameButton);
        
        container.addComponent(connectScreen, 1);
        connectScreen.addComponent(new LLabel("IP Address:"));
        connectScreen.addComponent(addressField);
        connectScreen.addComponent(connectOkayButton);
        connectScreen.addComponent(cancelButton);
        
        container.addComponent(nameScreen, 2);
        nameScreen.addComponent(new LLabel("New name:"));
        nameScreen.addComponent(nameField);
        nameScreen.addComponent(nameOkayButton);
        nameScreen.addComponent(cancelButton);
    }
    @Override
    public void buttonClicked(ButtonEvent e) {
        LComponent c = e.getSource();
        if(c == cancelButton){
            container.showComponent(0);
        }else if(c == networkButton){
            container.showComponent(1);
        }else if(c == nameButton){
            container.showComponent(2);
        }else if(c == nameOkayButton || c == nameField){
            //zmenit jmeno hrace
            logic.changeLocalPlayerName(nameField.getText());
        }else if(c == connectOkayButton  || c == addressField){
            //pripojit k serveru
            logic.networkConnect(addressField.getText());
        }else if(c == exitGameButton){
            //ukoncit DPDS
            logic.exitGame();
        }
    }
    
}
